package com.example.mediamoudle.recycle.activity

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.baselibrary.base.BaseActivity
import com.android.baselibrary.base.BaseAdapter
import com.example.mediamoudle.R
import com.example.mediamoudle.databinding.ActivityRecycleViewMainBinding
import com.example.mediamoudle.databinding.MediaItemPalyerBinding
import com.example.mediamoudle.recycle.adapter.RecycleViewMedialPlayerAdapter
import com.example.mediamoudle.recycle.utils.OnViewPagerListener
import com.example.mediamoudle.recycle.utils.PagerLayoutManager
import com.example.mediamoudle.router.MediaRouterPath
import com.example.mediamoudle.utlis.MediaDataUtils

@Route(path = MediaRouterPath.MEDIA_RECYCLEVIEW)
class RecycleViewMainActivity : BaseActivity<ActivityRecycleViewMainBinding>() {
    private lateinit var viewPagerLayoutManager: PagerLayoutManager
    override fun getLayoutId(): Int =R.layout.activity_recycle_view_main

    override fun initView() {
         viewPagerLayoutManager= PagerLayoutManager(this)
        mBinding.recycleMediaPlayer.layoutManager = viewPagerLayoutManager
        mBinding.recycleMediaPlayer.adapter = RecycleViewMedialPlayerAdapter().apply {
            setData(MediaDataUtils.dataList)
        }
    }

    override fun initListener() {
            viewPagerLayoutManager.setOnViewPagerListener(object :OnViewPagerListener{
                override fun onInitComplete() {
                    val holder = mBinding.recycleMediaPlayer.findViewHolderForLayoutPosition(0) as BaseAdapter<String, MediaItemPalyerBinding>.BaseViewHolder
                    holder.binding.standardPlayerItem.startPlayLogic()
                }

                override fun onPageRelease(isNext: Boolean, position: Int, view: View?) {
                    val holder = mBinding.recycleMediaPlayer.findViewHolderForLayoutPosition(position) as BaseAdapter<String, MediaItemPalyerBinding>.BaseViewHolder
                    holder.binding.standardPlayerItem.onVideoPause()
                }

                override fun onPageSelected(position: Int, bottom: Boolean, view: View?) {
                   val holder = mBinding.recycleMediaPlayer.findViewHolderForLayoutPosition(position) as BaseAdapter<String, MediaItemPalyerBinding>.BaseViewHolder
                       holder.binding.standardPlayerItem.startPlayLogic()
                }

            })
    }
}